package com.spj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spj.entity.QueryCondition;
import com.spj.entity.ResponseInfo;
import com.spj.entity.Role;
import com.spj.entity.User;
import com.spj.service.impl.PermServiceDaoImpl;
import com.spj.service.impl.RoleServiceDaoImpl;
import com.spj.service.impl.UserServiceDaoImpl;
import com.spj.shiro.JwtUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :spj
 * @Date:2022/4/17
 * @description:
 */
@RestController
@RequestMapping("/userCenter")
public class UserController {
    @Autowired
    UserServiceDaoImpl userService;
    @Autowired
    RoleServiceDaoImpl roleService;
    @Autowired
    PermServiceDaoImpl permService;

    //登录生成token
    @PostMapping("/login")
    public ResponseInfo loginDemo(String username, String password) {
        User user = userService.checkUser(username, password);
        if (user == null) {
            return new ResponseInfo(400,"登录失败","账号密码错误");
        }else {
            String token = JwtUtil.sign(username, password);
            return new ResponseInfo(200, "登录成功", token);
        }
    }
    //分页查询用户
    @RequiresPermissions(value = {"user:select"},logical = Logical.OR)
    @PostMapping("/user/{page}/{size}")
    public ResponseInfo findAllUsers(@PathVariable int page, @PathVariable int size, @RequestBody QueryCondition queryCondition) {
        PageHelper.startPage(page,size);
        List<User> users = userService.findtAllUsers(queryCondition);
        PageInfo<User> userPageInfo=new PageInfo<>(users);
        return new ResponseInfo(200, "管理员功能:查询所有用户", userPageInfo);
    }
    //401错误
    @GetMapping("/wrong401")
    public ResponseInfo wrong401() {
        return new ResponseInfo(401, "认证异常", "");
    }
    //无消息头
    @GetMapping("/noHeader")
    public ResponseInfo noHeader() {
        return new ResponseInfo(402, "无请求头", "");
    }
    //删除用户
    @DeleteMapping("/user/{id}")
    public ResponseInfo deleteUser(@PathVariable int id){
        userService.deleteUserById(id);
        return new ResponseInfo(200, "管理员功能:删除用户", "删除成功");
    }
    //查询所有角色
    @RequiresPermissions(value = {"user:select"},logical = Logical.OR)
    @GetMapping("/role")
    public ResponseInfo getAllRoles() {
        List<Role> roles = roleService.findtAllRoles();
        return new ResponseInfo(200, "管理员功能:查询角色", roles);
    }
    //修改用户
    @RequiresPermissions(value = {"user:select"},logical = Logical.OR)
    @PutMapping("/user")
    public ResponseInfo updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return new ResponseInfo(200, "管理员功能:修改用户","修改成功" );
    }
    //修改用户
    @RequiresPermissions(value = {"user:select"},logical = Logical.OR)
    @PostMapping("/user")
    public ResponseInfo addUser(@RequestBody User user) {
        userService.addUser(user);
        return new ResponseInfo(200, "管理员功能:增加用户","添加成功" );
    }
}
